 <?php
/**
 * 后台，用户管理控制器
 * @author liuxiaoqing <liuxiaoqing@zhishisoft.com>
 * @version TS3.0
 */
// 加载后台控制器
tsload ( APPS_PATH . '/admin/Lib/Action/AdministratorAction.class.php' );
class ShopAction extends AdministratorAction {

	public $pageTitle = array();
	
	/**
	 * 初始化，初始化页面表头信息，用于双语
	 */
	public function _initialize() {
		$this->pageTitle['index'] = '商品列表';
		$this->pageTitle['addshop'] = '添加商品';
		//$this->pageTitle['convert'] = '兑换记录';
		//$this->pageTitle['update'] = '升级软件';
		parent::_initialize();
	}

	/**
	 * 微吧列表
	 * @return void
	 */
	public function index() {//【商品列表】
		// 初始化微吧列表管理菜单
		$this->_initWeibaListAdminMenu();
		$this->searchKey = array('pid','product_name',array('credit','credit1'),'shop_category_id','status',array('endtime','endtime1'));
		$this->opt['status'] =array('0'=>'全部','1'=>'已过期','2'=>'出售中');
		$this->pageButton[] = array('title'=>'搜索商品','onclick'=>"admin.fold('search_form')");
		$this->pageButton[] = array('title'=>L('PUBLIC_REMOVE_COMPLETELY'),'onclick'=>"admin.delselectshop('','deleteShop','".L('PUBLIC_REMOVE_COMPLETELY')."','".L('商品')."')");
		$map['pid'] = array('neq','0');
		$category = M('Shop_category')->where($map)->select();
		foreach ($category as $cate){
		$category_array[$cate['shop_category_id']] = $cate['title'];
		}
		$category_array['0']='全部';
		$this->opt['shop_category_id'] = $category_array;
		// 设置列表主键
		$this->_listpk = 'pid';
		//$this->searchKey = array('weiba_id','weiba_name','uid','admin_uid','recommend');
		$this->pageKeyList = array('pid','product_name','product_ico','product_num','credit','sold','endtime','status','shop_category_id','DOACTION');
		// 数据的格式化与listKey保持一致
		$listData = D('shop')->getWeibaList(20);
		$this->displayList($listData);
	}
	
	public function convert(){
		$this->_initWeibaListAdminMenu();
		$this->pageKeyList = array('shop_category_id','uid','pid','dateline','product_num','get','setget');
		$listData = D('shop_convert')->findPage(20);
		foreach($listData['data'] as $k => $v) {
			$username = D('user')->where('uid='.$listData['data'][$k]['uid'])->find();
			$listData['data'][$k]['uid'] = $username['uname'];
			$listData['data'][$k]['dateline'] = date('Y-m-d h:i:s',$listData['data'][$k]['dateline']);
			switch($v['get']){
				case 0:$listData['data'][$k]['get'] = '未领取';break;
				case 1:$listData['data'][$k]['get'] = '已领取';break;
			}
			$listData['data'][$k]['setget'] = "<a onclick='admin.setget(".$v['shop_category_id'].")'>领取</a>";
		}
		$this->displayList($listData);
	}
	
	public function setget(){
		$data['get'] = 1;
		$shop_category_id = $_POST['shop_category_id'];
		$setstatus = D('shop_convert')->where('shop_category_id='.$shop_category_id)->save($data);
		if($setstatus){
			$data['status'] = 1;
			$data['data'] = "领取成功";
		}
		exit(json_encode($data));
	}

	public function addshop() {//【添加商品】
		// 初始化微吧列表管理菜单
		$this->_initWeibaListAdminMenu();
        // 列表key值 DOACTION表示操作
		$credit_type = D('credit_type')->findAll();
		//var_dump($credit_type);
		foreach($credit_type as $k => $v){
			$this->opt['credit_type'][$v['name']] = $v['alias'];
		}
		$this->pageKeyList = array('product_name','product_ico','product_num','use_cont','credit','shop_category_id','endtime');
		
		$map['pid'] = array('neq','0');
		$category = M('Shop_category')->where($map)->select();
		foreach ($category as $cate){
		$category_array[$cate['shop_category_id']] = $cate['title'];
		}
		$category_array[0]='请选择分类';
		$this->opt['shop_category_id'] = $category_array;
		$this->savePostUrl = U('admin/Shop/doAddshop');
        $this->notEmpty = array("product_name","product_ico","shop_category_id","endtime");
		$this->onsubmit = 'admin.addshopSubmitCheck(this)';
		$this->displayConfig();
	}
	
	public function doAddshop() {//【执行添加商品】
		$Shop_product = M(Shop_product);
		$Shop_product->product_name = $product_name = t($_POST['product_name']);
		$Shop_product->product_ico = t($_POST['product_ico']);
		$Shop_product->product_num = t($_POST['product_num']);
		$Shop_product->use_cont = $_POST['use_cont'];
		//$Shop_product->credit_type = $_POST['credit_type'];
		$Shop_product->shop_category_id = $_POST['shop_category_id'];
		$Shop_product->credit = $_POST['credit'];
		$Shop_product->endtime = strtotime($_POST['endtime']);
		
		if($Shop_product->where("product_name = '$product_name'")->find()){
			$this->assign('jumpUrl', U('admin/Shop/addshop'));
			$this->error('商品名已存在');
		
		}
		
		//dump($Shop_product);exit;
		$res = $Shop_product->add();
		if($res){
			$this->assign('jumpUrl', U('admin/Shop/index'));
			$this->success(L('PUBLIC_ADD_SUCCESS'));
		}else{
			$this->assign('jumpUrl', U('admin/Shop/addshop'));
			$this->error('添加失败');
		}
	}
	
	public function editshop() {//【编辑商品】
		$this->assign('pageTitle','编辑商品');
		// 初始化微吧列表管理菜单
		$this->pageTab[] = array('title'=>'商品列表','tabHash'=>'index','url'=>U('admin/Shop/index'));
		$this->pageTab[] = array('title'=>'添加商品','tabHash'=>'addshop','url'=>U('admin/Shop/addshop'));
		//$this->pageTab[] = array('title'=>'兑换记录','tabHash'=>'postList','url'=>U('admin/Shop/postList'));
        // 列表key值 DOACTION表示操作
		$credit_type = D('credit_type')->findAll();
		foreach($credit_type as $k => $v){
			$this->opt['credit_type'][$v['name']] = $v['alias'];
		}
		$this->pageKeyList = array('pid','product_name','product_ico','product_num','use_cont','credit_type','credit','shop_category_id','endtime');
		$weiba_id = intval($_GET['pid']);
		$data = D('shop')->getshopById($weiba_id);
		
		$map['pid'] = array('neq','0');
		$category = M('Shop_category')->where($map)->select();
		foreach ($category as $cate){
		$category_array[$cate['shop_category_id']] = $cate['title'];
		}
		
		$this->opt['shop_category_id'] = $category_array;
		// 表单URL设置
		$this->savePostUrl = U('admin/Shop/doEditshop');
        $this->notEmpty = array("product_name","product_ico","shop_category_id","endtime");
		$this->onsubmit = 'admin.addshopSubmitCheck(this)';
		$this->displayConfig($data);
	}
	
	public function doEditshop(){//【编辑保存商品】
		$weiba_id = intval($_POST['pid']);
		$data['product_name'] = $product_name = t($_POST['product_name']);
		//$data['uid'] = $this->mid;
		$data['product_ico'] = t($_POST['product_ico']);
		$data['product_num'] = t($_POST['product_num']);
		$data['use_cont'] = $_POST['use_cont'];
		$data['credit_type'] = t($_POST['credit_type']);
		$data['shop_category_id'] = t($_POST['shop_category_id']);
		$data['credit'] = t($_POST['credit']);
		$data['endtime'] = strtotime($_POST['endtime']);
		
		$map['product_name'] = array('eq',$product_name);
		$map['pid'] = array('neq',$weiba_id);
		
		if(M('Shop_product')->where($map)->find()){
			$this->assign('jumpUrl', U('admin/Shop/index'));
			$this->error('商品名已存在');
		
		}
			
		$res = D('shop')->where('pid='.$weiba_id)->save($data);
		if($res){
			$this->assign('jumpUrl', U('admin/Shop/index'));
			$this->success(L('修改成功'));
		}else{
			$this->assign('jumpUrl', U('admin/Shop/index'));
			$this->error(L('修改失败'));
		}
	}

	public function delshop(){//【删除商品】
		$pid = $_POST['pid'];
		$map['pid'] = is_array($pid) ? array('IN', $pid) : intval($pid);
		$result = M('Shop_product')->where($map)->delete();
			$return['status'] = 1;
			$return['data']   = L('PUBLIC_ADMIN_OPRETING_SUCCESS');
		echo json_encode($return);exit();
	}
	
	
	public function update() {
		// 初始化微吧列表管理菜单
		$this->_initWeibaListAdminMenu();
		$this->opt['doupdate'] = array('1'=>'是','0'=>'否');
		$this->pageKeyList = array('doupdate');
		$this->savePostUrl = U('admin/Shop/doupdate');
		$this->displayConfig();
	}
	
	public function doupdate(){
		if($_POST['doupdate']==1){
			header('Content-Type: text/html; charset=utf-8');
			$sql_file  = APPS_PATH.'/shop/Appinfo/update.sql';
			//执行sql文件
			$res = D('')->executeSqlFile($sql_file);
			if(!empty($res)){
				$this->error('升级失败');
			}else{
				$this->success('升级成功');
			}
		}else{
			$this->success('取消升级成功');
		}
	}
	
	/**
	 * 产品分类
	 * @return void
	 */
//	public function category(){//【商品分类列表】
//		$this->assign('pageTitle','分类列表');
//		$this->searchKey = array('shop_category_id','title','status');
//		$this->pageButton[] = array('title'=>'搜索分类','onclick'=>"admin.fold('search_form')");
//		$this->pageButton[] = array('title'=>L('PUBLIC_REMOVE_COMPLETELY'),'onclick'=>"admin.delselectCategory('','deleteCategory','".L('PUBLIC_REMOVE_COMPLETELY')."','".L('商品分类')."')");
//		$this->opt['status'] =array('0'=>'全部','1'=>'关闭','2'=>'开启');
//		$map=array();
//		if(isset($_POST)) {
//			//搜索时用到
//			$_POST['shop_category_id'] && $map['shop_category_id']=intval($_POST['shop_category_id']);
//			$_POST['title'] && $map['title']=array('like','%'.$_POST['title'].'%');
//			
//			$_POST['status'] && $map['status']=$_POST['status'];
//			if($_POST['status']==1){$map['status']=0;}elseif($_POST['status']==2){$map['status']=1;}else{unset($map['status']);}
//			
//		}	
//		//dump($map);exit;
//		$this->pageTab[] = array('title'=>'分类列表','tabHash'=>'category','url'=>U('admin/Shop/category'));
//		$this->pageTab[] = array('title'=>'添加分类','tabHash'=>'addCategory','url'=>U('admin/Shop/addCategory'));
//		$this->_listpk = 'shop_category_id';
//		$this->pageKeyList = array('shop_category_id','title','description','status','DOACTION');
//		$listData = D('shop')->getCategory(20,$map);
//		$this->displayList($listData);
//	}
	public function category(){//【商品分类列表】
		$treeData = model('CategoryTree')->settable('shop_category')->getNetworkList();
    	//dump($treeData);exit;
    	$extra = array(  'show_type'=>array('图片/文字','图片', '文字') );
    	$channelConf = model('Xdata')->get('admin_News:index');
    	$defaultExtra = array('show_type'=>$channelConf['show_type']);
    	$extra = encodeCategoryExtra($extra, $defaultExtra);
    	// 配置删除关联信息
    	$delParam['app'] = 'news';
    	$delParam['module'] = 'News';
    	$delParam['method'] = 'deleteAssociatedData';
    	$this->shopCategory($treeData, 'shop_category', 2, $delParam);
	}
	public function addCategory(){//【添加商品分类】
		$this->assign('pageTitle','添加分类');
		// 初始化微吧列表管理菜单
		$this->pageTab[] = array('title'=>'分类列表','tabHash'=>'category','url'=>U('admin/Shop/category'));
		$this->pageTab[] = array('title'=>'添加分类','tabHash'=>'addCategory','url'=>U('admin/Shop/addCategory'));
		
		$this->pageKeyList = array('title','description','status');
		$this->opt['status'] = array('1'=>'开启','2'=>'关闭');
		$this->savePostUrl = U('admin/Shop/doAddCategory');
        //$this->onsubmit = 'admin.checkAddWeiba(this)';
		$this->notEmpty = array("title");
		$this->onsubmit = 'admin.addCateSubmitCheck(this)';
		$this->displayConfig();
		
	}
	
	public function doAddCategory() {//【执行添加商品分类】
		$Shop_category = M(Shop_category);
		$Shop_category->title = $title = t($_POST['title']);
		$Shop_category->description = t($_POST['description']);
		$Shop_category->status = t($_POST['status']);
		if($Shop_category->where("title = '$title'")->find()){
			$this->assign('jumpUrl', U('admin/Shop/addCategory'));
			$this->error('分类名已存在');
		}
		$res = $Shop_category->add();
		if($res){
			$this->assign('jumpUrl', U('admin/Shop/category'));
			$this->success('添加成功');
		}else{
		
			$this->assign('jumpUrl', U('admin/Shop/addCategory'));
			$this->error('添加失败');
		}
	}
	
	
	public function editCategory() {//【编辑商品分类】
		$this->assign('pageTitle','编辑分类');
		// 初始化微吧列表管理菜单
		//$this->pageTab[] = array('title'=>'分类列表','tabHash'=>'category','url'=>U('admin/Shop/category'));
		//$this->pageTab[] = array('title'=>'添加分类','tabHash'=>'addCategory','url'=>U('admin/Shop/addCategory'));
        // 列表key值 DOACTION表示操作
		
		$this->pageKeyList = array('shop_category_id','title','description','status');
		$this->opt['status'] = array('1'=>'开启','2'=>'关闭');
		$shop_category_id = intval($_GET['shop_category_id']);
		$data = M('Shop_category')->where('shop_category_id='.$shop_category_id)->find();
		// 表单URL设置
		$this->savePostUrl = U('admin/Shop/doEditCategory');
        $this->notEmpty = array("title");
		$this->onsubmit = 'admin.addCateSubmitCheck(this)';
		$this->displayConfig($data);
	}
	
	public function doEditCategory(){//【保存编辑商品分类】
		$shop_category_id = intval($_POST['shop_category_id']);
		$data['title'] = $title = t($_POST['title']);
		//$data['uid'] = $this->mid;
		$data['description'] = t($_POST['description']);
		$data['product_num'] = t($_POST['product_num']);
		$data['status'] = $_POST['status'];
		
		$map['title'] = array('eq',$title);
		$map['shop_category_id'] = array('neq',$shop_category_id);
		
		if(M('Shop_category')->where($map)->find()){
			$this->assign('jumpUrl', U('admin/Shop/category'));
			$this->error('分类名已存在');
		
		}
		
		
		$res = M('Shop_category')->where('shop_category_id='.$shop_category_id)->save($data);
		if($res){
			$this->cleanCache();
			
			$this->assign('jumpUrl', U('admin/Shop/category'));
			$this->success('修改成功');
		}else{
		
			$this->assign('jumpUrl', U('admin/Shop/editCategory'));
			$this->error('修改失败');
		}
	}
	/**
	 * 分类关闭使用操作
	 * @return json 操作后的JSON数据
	 */
	public function disCategory() {//【关闭商品分类】
		if(empty($_POST['id'])) {
			$return['status'] = 0;
			$return['data'] = '';
			exit(json_encode($return));
		}

		//$result = model('User')->deleteUsers(intval($_POST['id']));
		$Shop_category = M(Shop_category);
		$Shop_category->status = 0;
		$Shop_category->shop_category_id  =$_POST['id'];
		$result = $Shop_category->save();
		if(!$result) {
			$return['status'] = 0;
			$return['data'] = L('PUBLIC_ADMIN_OPRETING_ERROR');				// 操作失败
		} else {
			$this->cleanCache();
			// 关联删除用户其他信息，执行删除用户插件.
			$return['status'] = 1;
			$return['data'] = L('PUBLIC_ADMIN_OPRETING_SUCCESS');			// 操作成功
			
		}
		exit(json_encode($return));
	}
	
	/**
	 * 分类恢复启用操作
	 * @return json 操作后的JSON数据
	 */
	public function rebackCategory() {//【开启商品分类】
		if(empty($_POST['id'])) {
			$return['status'] = 0;
			$return['data'] = '';
			exit(json_encode($return));
		}
		$Shop_category = M(Shop_category);
		$Shop_category->status = 1;
		$Shop_category->shop_category_id  =$_POST['id'];
		$result = $Shop_category->save();
		if(!$result) {
			$return['status'] = 0;
			$return['data'] = L('PUBLIC_ADMIN_OPRETING_ERROR');				// 操作失败
		} else {
			$this->cleanCache();
			// 关联删除用户其他信息，执行删除用户插件.
			$return['status'] = '1';
			$return['data'] = L('PUBLIC_ADMIN_OPRETING_SUCCESS');			// 操作成功
		}
		exit(json_encode($return));
	}
	
	
	public function delCategory(){
		$shop_category_id = $_POST['shop_category_id'];
		$map['shop_category_id'] = is_array($shop_category_id) ? array('IN', $shop_category_id) : intval($shop_category_id);
		$result = M('Shop_category')->where($map)->delete();
			$return['status'] = 1;
			$return['data']   = L('PUBLIC_ADMIN_OPRETING_SUCCESS');
		echo json_encode($return);exit();
	
	}
	public function cleanCache(){
		$dirs = array ('./_runtime/');foreach ( $dirs as $value ) {$tool = new ToolAction();$tool->_rmdirr ( $value );}
			@mkdir ( '_runtime', 0777, true );
			model('Cache')->clear();
	}
	
	/**
	 * 微吧后台管理菜单
	 * @return void
	 */
	private function _initWeibaListAdminMenu(){
		$this->pageTab[] = array('title'=>'商品列表','tabHash'=>'index','url'=>U('admin/Shop/index'));
		$this->pageTab[] = array('title'=>'添加商品','tabHash'=>'addshop','url'=>U('admin/Shop/addshop'));
		//$this->pageTab[] = array('title'=>'兑换记录','tabHash'=>'postList','url'=>U('admin/Shop/convert'));
		//$this->pageTab[] = array('title'=>'升级软件','tabHash'=>'update','url'=>U('admin/Shop/update'));
	}
}